//
//  MTEditTagViewController.swift
//  MT
//
//  Created by M on 2020/1/5.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory
import YPNavigationBarTransition

protocol MTEditTagViewControllerDelegate: NSObjectProtocol {    
    ///
    func editTagViewController(_ controller: MTEditTagViewController, didEditTagWithName name: String)
}

class MTEditTagViewController: BaseViewController {
    
    weak var delegate: MTEditTagViewControllerDelegate?

    lazy var textField: UITextField = {
        let tf = UITextField()
        let att_string = NSMutableAttributedString(string: "请填写")
        att_string.setAttributes([NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.5),
                                  NSAttributedString.Key.font: PingFangSCMediumFont(14)], range: NSMakeRange(0, att_string.length))
        tf.attributedPlaceholder = att_string
        tf.tintColor = .white
        tf.textColor = .white
        tf.font = PingFangSCMediumFont(14)
        return tf
    }()
    
    /// underline
    lazy var underline: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    var tagType: MTTagViewController.TagType = .personality
    
}

// MARK: ------------------------------------ Overrides ----------------------------------------
extension MTEditTagViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        initializeUI()
    }
    
}


// MARK: ---------------------------------------- 初始化 --------------------------------------------
extension MTEditTagViewController {
    
    private func setup() {
        view.backgroundColor = UIColor(hex: 0x161616)
        let r_btn = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
        r_btn.setBackgroundImage(UIImage(color: UIColor(hex: 0x5d5d5d)), for: .disabled)
        r_btn.setBackgroundImage(UIImage(color: UIColor.theme.main), for: .normal)
        r_btn.setTitle("保存", for: .normal)
        r_btn.setTitleColor(.black, for: .normal)
        r_btn.titleLabel?.font = PingFangSCSemiboldFont(14)
        r_btn.adjustsImageWhenHighlighted = false
        r_btn.layer.cornerRadius = 3
        r_btn.clipsToBounds = true
        r_btn.addTarget(self, action: #selector(editTagViewController(didClickSaveButton:)), for: .touchUpInside)
        let rightButton = UIBarButtonItem(customView: r_btn)
        self.navigationItem.rightBarButtonItem = rightButton
        
        self.navigationItem.title = tagType == .personality ? "编辑标签" : "编辑兴趣"
    }
    
    private func initializeUI() {
        view.addSubview(textField)
        view.addSubview(underline)
        
        textField.snp.makeConstraints { (make) in
            make.left.equalTo(view).offset(16)
            make.top.equalTo(view).offset(16+NavigationBarHeight+StatusBarHeight)
            make.right.equalTo(view).offset(-16)
            make.height.equalTo(40)
        }
        
        underline.snp.makeConstraints { (make) in
            make.left.right.equalTo(textField)
            make.top.equalTo(textField.snp.bottom)
            make.height.equalTo(1)
        }
        
        textField.becomeFirstResponder()
    }
    
}


// MARK: ---------------------------------------- 事件方法 --------------------------------------------
@objc
extension MTEditTagViewController {
    
    private func editTagViewController(didClickSaveButton sender: UIBarButtonItem) {
        guard let text = textField.text, text != "" else {
            return
        }
        if let delegate = delegate {
            delegate.editTagViewController(self, didEditTagWithName: text)
        }
        self.navigationController?.popViewController(animated: true)
    }
    
}

